import 'dart:async';

import 'package:agora_rtc_engine/agora_rtc_engine.dart';
import 'package:get/get.dart';

import 'room_music_center_controller.dart';

/// 搜索音乐列表
class RoomMusicSearchController extends GetxController {
  late RoomMusicCenterController centerController;

  int page = 0;
  final int pageSize = 20;

  MusicCollection? data;
  StreamSubscription? subscription;

  RoomMusicSearchController();

  @override
  void onInit() {
    super.onInit();
    centerController = Get.find<RoomMusicCenterController>();
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
    subscription?.cancel();
  }

  Future<void> search(String keyword) async {
    String requestId =
        await centerController.searchMusicList(keyword, page: page);
    subscription =
        centerController.subscribeMusicCollection(requestId).listen((event) {
      data = event.result;
      update();
    });
  }

  int get count => data?.getCount() ?? 0;

  void dismiss() {
    data = null;
  }
}
